package com.ptit.piclient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.ptit.piclient.sp.SPUtils;
import com.ptit.piclient.tools.ImageAdapter;
import com.ptit.piclient.tools.PiConnector;

public class MainActivity extends Activity {
	//Oct 8
	class CheckSettingInSP extends AsyncTask<Void, Void, Boolean>{
		private Activity act;
		public CheckSettingInSP(Activity a){
			act = a;
		}
		@Override
		protected Boolean doInBackground(Void... params) {
			boolean b = SPUtils.checkSettings(act);
			return b;
		}
		@Override
		public void onPostExecute(Boolean check){
			if (check == false) {
				startActivity(new Intent(act, SettingsActivity.class));
			} else{
				act.setContentView(R.layout.activity_main);
				GridView gridView = (GridView) act.findViewById(R.id.grid_view_main);
				gridView.getRootView().setBackgroundColor(android.R.color.black);		
//				new UpdateView(act).execute();
				
				// Instance of ImageAdapter Class
				gridView.setAdapter(new ImageAdapter(act));
				gridView.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View v,
							int position, long id) {
						TextView tv = (TextView) v.findViewById(R.id.grid_item_label);
						String label = tv.getText().toString();
						if (label.equals("All Song")) {
							Intent i = new Intent(getApplicationContext(),
									AllSongActivity.class);
							i.putExtra("isPlayingAllSong", true);
							startActivity(i);
						}
						if (label.equals("Albums")) {
							Intent i = new Intent(getApplicationContext(),
									AllAlbumActivity.class);
							i.putExtra("isPlayingAllSong", false);
							startActivity(i);
						}
						if (label.equals("Artists")) {
							Intent i = new Intent(getApplicationContext(),
									AllArtistActivity.class);
							i.putExtra("isPlayingAllSong", false);
							startActivity(i);
						}
						if (label.equals("Settings")) {
							Intent i = new Intent(getApplicationContext(),
									SettingsActivity.class);
							i.putExtra("isPlayingAllSong", false);
							startActivity(i);
						}
					}
				});
			}
		}
	}
	// End oct 8
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Oct 8
		new CheckSettingInSP(this).execute();		
		//End Oct 8		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_for_main_activity, menu);
		new CheckIfGoToNowPlayingIsAvailable(this,menu).execute();
		return true;
	}
	// Oct 8
	class CheckIfGoToNowPlayingIsAvailable extends AsyncTask<Void, Void, Boolean>{
		Activity act;
		Menu menu;
		public CheckIfGoToNowPlayingIsAvailable(Activity ac, Menu m){
			menu = m;
			act = ac;
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			
			return null;
		}
		@Override
		public void onPostExecute(Boolean check){
			
		}
	}
	// Oct 8
	// khuongdv start Sept 28
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuItemUpdateLibrary:
			updateLibrary();
			return true;

			// ... other item selection handling
		case R.id.menuItemGotoNowPlaying:
			gotoNowPlaying();
			return true;
		default:
			return super.onOptionsItemSelected(item);

		}
	}

	private void updateLibrary() {
		new AudioLibraryScan(this).execute();
	}

	private class AudioLibraryScan extends AsyncTask<Void, Void, Void> {
		private Activity act;

		public AudioLibraryScan(Activity a) {
			act = a;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			PiConnector.getInstance(act).audioLibraryScan();
//			new UpdateView(act).execute();
			return null;
		}

		@Override
		protected void onProgressUpdate(Void... params) {
			ProgressDialog dialog = new ProgressDialog(MainActivity.this);// ,
																			// "",
																			// "Please wait...",true);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();

		}

		// khuongdv start sep28
		@Override
		protected void onPostExecute(Void result) {
			Toast.makeText(act, "Done!", Toast.LENGTH_LONG).show();
		}
		// khuongdv end sep28
	}

	private void gotoNowPlaying() {
		Intent i = new Intent(getApplicationContext(), NowPlayingActivity.class);
		// i.putExtra("isPlayingAllSong", false);
		startActivity(i);
	}

	// khuongdv end
//	private class UpdateView extends AsyncTask<Void, Void, Void> {
//		Activity act;
//
//		public UpdateView(Activity a) {
//			act = a;
//		}
//
//		@Override
//		protected Void doInBackground(Void... params) {
//			ArrayList<Song> list = PiConnector.getInstance(act)
//					.getAllSongsFromPI();
//			PiConnector.getInstance(act).addToPlaylist();
//			return null;
//		}
//
//		@Override
//		protected void onPreExecute() {
//
//		}
//
//		protected void onPostExecute(Void result) {
//			if (result != null);
//		}
//
//	}
	//khuongdv start Oct 4
	@Override
	public void onResume(){
		super.onResume();
		setContentView(R.layout.activity_main);
		GridView gridView = (GridView) findViewById(R.id.grid_view_main);

		gridView.getRootView().setBackgroundColor(android.R.color.black);
//		new UpdateView(this).execute();
		// Instance of ImageAdapter Class
		gridView.setAdapter(new ImageAdapter(this));
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				TextView tv = (TextView) v.findViewById(R.id.grid_item_label);
				String label = tv.getText().toString();
				if (label.equals("All Song")) {
					Intent i = new Intent(getApplicationContext(),
							AllSongActivity.class);
					i.putExtra("isPlayingAllSong", true);
					startActivity(i);
				}
				if (label.equals("Albums")) {
					Intent i = new Intent(getApplicationContext(),
							AllAlbumActivity.class);
					i.putExtra("isPlayingAllSong", false);
					startActivity(i);
				}
				if (label.equals("Artists")) {
					Intent i = new Intent(getApplicationContext(),
							AllArtistActivity.class);
					i.putExtra("isPlayingAllSong", false);
					startActivity(i);
				}
				if (label.equals("Settings")) {
					Intent i = new Intent(getApplicationContext(),
							SettingsActivity.class);
					i.putExtra("isPlayingAllSong", false);
					startActivity(i);
				}
			}
		});
	}
	//khuongdv start Oct 4
}
